package com.flowerSystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.flowerSystem.entity.Collection;
import com.flowerSystem.entity.Productinformation;
import com.flowerSystem.service.CollectionService;
import com.flowerSystem.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 鲜花遇上牛战士
 * @since 2023-08-28
 */
@RestController
@RequestMapping("/flower")
@CrossOrigin
public class CollectionController {
    @Autowired
    CollectionService collectionService;
    //用户添加收藏
    @PostMapping("/collection")
    public ResponseResult postCollectionByUserId(Collection collection){
        boolean f = collectionService.save(collection);
        if (f){
            return ResponseResult.ok(collection);
        }
        return ResponseResult.fail();
    }

    //用户删除收藏
    @DeleteMapping("/collection")
    public ResponseResult deleteCollection(Collection collection){
        boolean f = collectionService.removeById(collection);
        if (f){
           return ResponseResult.ok(collection);
        }
        return ResponseResult.fail();
    }
    //根据用户查看收藏
    @GetMapping("/collection")
    public ResponseResult getCollectionByUser(int userId){
        List<Productinformation> list = collectionService.getCollectionByUser(userId);
        return ResponseResult.ok(list);
    }
    //查询用户是否收藏
    @GetMapping("/userCollection")
    public ResponseResult getUserCollection(@RequestParam int userId,@RequestParam int productinformationId){
       boolean f = collectionService.getUserCollection(userId,productinformationId);
        if (f){
           return ResponseResult.ok(1);
        }

        return ResponseResult.fail();
    }



}
